SHELL = /bin/sh
CHMOD = chmod

version = 2.0
package = vitamina

# These will be filled in by configure
prefix = @prefix@
exec_prefix = $(prefix)
bindir = $(exec_prefix)/bin
libexecdir = $(exec_prefix)/lib
datarootdir = $(prefix)/share
datadir = $(datarootdir)

# Library files to install
libfiles = \
	lib/gawkldr \
	lib/core \
	lib/utils.awk \
	lib/logs \
	lib/guess

# The default target
all: VitaminA $(libfiles)

# Delete everything we've created
clean:
	-rm VitaminA

# Also delete files configure has created
distclean: clean
	-rm Makefile configure.app

# Configure the launcher script
VitaminA: VitaminA.in
	# Set the paths
	sed "s|^[ \t]*prefix=|&$(prefix)|" \
		VitaminA.in > VitaminA
	$(CHMOD) 755 VitaminA

# Preprocess files

# Alternative rule for incompatible make versions
#.SUFFIXES:
#.SUFFIXES: .app
#.app:
%:: %.app configure.app utils/app.awk
	awk -f utils/app.awk configure.app $< > $@
	$(CHMOD) --reference=$< $@

# Install the software
install: all
	for dir in $(bindir) $(libexecdir)/$(package) $(datadir)/$(package); do \
		mkdir -p $(DESTDIR)$$dir; \
	done
	cp VitaminA $(DESTDIR)$(bindir)
	cp -r $(libfiles) $(DESTDIR)$(libexecdir)/$(package)
	cp -r share/* $(DESTDIR)$(datadir)/$(package)
	
	# Remove the .svn nonsense
	find $(DESTDIR)$(prefix) -type d -name .svn -print0 | xargs -0 rm -rf

# Uninstall the software
uninstall:
	for item in $(bindir)/VitaminA $(libexecdir)/$(package) $(datadir)/$(package); do \
		rm -rf $$item; \
	done

# Make a source tarball
dist:
	mkdir $(package)-$(version)
	cp -r configure Makefile.in VitaminA.in INSTALL LICENSE lib share utils \
		$(package)-$(version)
	tar czf $(package)-$(version).tar.gz $(package)-$(version) \
		--exclude=.svn
	
	# Clean up
	rm -rf $(package)-$(version)

# Make a .deb package
debian: all
	# Install into a subdirectory
	mkdir pkg
	$(MAKE) DESTDIR=pkg install
	
	# Generate some files
	echo "2.0" > debian-binary
	
	echo "Package: $(package)" > control
	echo "Version: $(version)" >> control
	echo "Section: net" >> control
	echo "Depends: gawk" >> control
	echo "Priority: optional" >> control
	echo "Architecture: all" >> control
	echo "Installed-Size: `du -ks pkg | cut -f 1`" >> control
	echo "Maintainer: Přemysl Janouch <p.janouch@gmail.com>" >> control
	echo "Homepage: http://vitamina.googlecode.com" >> control
	echo "Description: Modular IRC bot written in GNU AWK" >> control
	
	cd pkg && find * -type f -print0 | xargs -0 md5sum > ../md5sums
	tar czf control.tar.gz control md5sums
	
	# Pack the data
	cd pkg && tar czf ../data.tar.gz *
	
	# Generate the package
	ar r $(package)-$(version).deb \
		debian-binary control.tar.gz data.tar.gz
	
	# Clean up
	rm -rf pkg debian-binary control md5sums control.tar.gz data.tar.gz


.PHONY: all clean distclean install dist debian

